package com.lucian.ge.service.remind.controller;

import com.lucian.ge.service.remind.mapper.RemindMapper;
import com.lucian.ge.user.mapper.UserMapper;
import com.lucian.ge.comman.model.ResultMap;
import com.lucian.ge.util.JWTUtil;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;

/**
 * Created by Lucicol on 2019/5/20.
 */
@Api(value = "RemindController", description = "还款提醒")
@RestController
@RequestMapping("/remind")
public class RemindController {
    private final UserMapper userMapper;
    private final RemindMapper remindMapper;
    private final ResultMap resultMap;

    @Autowired
    public RemindController(UserMapper userMapper, ResultMap resultMap, RemindMapper remindMapper) {
        this.userMapper = userMapper;
        this.resultMap = resultMap;
        this.remindMapper= remindMapper;
    }

    /**
     * 还款信息
     */

    @ApiOperation(value="还款信息", notes="user/token")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="header", name = "user", value = "user", required = false, dataType = "String")
    })

    @GetMapping("/info")
    @RequiresRoles("user")
    public ResultMap info(@RequestHeader String token,@RequestHeader String user) {
        if (!JWTUtil.getUsername(token).equals(user)) {
            return resultMap.fail().code(401).message("用户非法请求");
        }
        List<Map<String, String>> remindInfo = remindMapper.getRemindInfo(user);
        return resultMap.success().code(200).message("成功获得还款信息").res(remindInfo);
    }

    /**
     * 修改还款信息
     */

    @ApiOperation(value="修改还款信息", notes="user/token")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="header", name = "user", value = "user", required = false, dataType = "String")
    })

    @PostMapping("/modify")
    @RequiresRoles("user")
    @ResponseBody
    public ResultMap modify(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) @Valid Remindinfo remindinfo, BindingResult result, @RequestHeader String token, @RequestHeader String user) {
        if (!JWTUtil.getUsername(token).equals(user)) {
        return resultMap.fail().code(401).message("用户非法请求");
        }

        //      参数校验
        if (result.hasErrors()) {
            return resultMap.fail().code(401).message(result.getFieldError().getDefaultMessage());
        }

        if (Integer.parseInt(remindinfo.getPayment_date()) < 1 || Integer.parseInt(remindinfo.getPayment_date()) > 31) {
            return resultMap.fail().code(401).message("非法还款日期");
        }
//        System.out.println(remindinfo.getPayment_target());
//        System.out.println(remindinfo.getPayment_date());
//        System.out.println(remindinfo.getId());
        remindMapper.modifyInfo(remindinfo.getPayment_target(), remindinfo.getPayment_date(), remindinfo.getId());
        return resultMap.success().code(200).message("成功修改还款信息");
    }


    /**
     * 新增还款信息
     */

    @ApiOperation(value="新增还款信息", notes="user/token")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="header", name = "user", value = "user", required = false, dataType = "String")
    })

    @PostMapping("/add")
    @RequiresRoles("user")
    @ResponseBody
    public ResultMap addInfo(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) @Valid Remindinfo remindinfo, BindingResult result, @RequestHeader String token, @RequestHeader String user) {
        if (!JWTUtil.getUsername(token).equals(user)) {
            return resultMap.fail().code(401).message("用户非法请求");
        }

        //      参数校验
        if (result.hasErrors()) {
            return resultMap.fail().code(401).message(result.getFieldError().getDefaultMessage());
        }

        if (Integer.parseInt(remindinfo.getPayment_date()) < 1 || Integer.parseInt(remindinfo.getPayment_date()) > 31) {
            return resultMap.fail().code(401).message("非法还款日期");
        }
        remindMapper.addInfo(remindinfo.getPayment_target(), remindinfo.getPayment_date(), remindinfo.getId(), user);
        return resultMap.success().code(200).message("成功新增还款信息");
    }

    /**
     * 删除还款信息
     */

    @ApiOperation(value="删除还款信息", notes="user/token")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="header", name = "user", value = "user", required = false, dataType = "String")
    })

    @PostMapping("/del")
    @RequiresRoles("user")
    @ResponseBody
    public ResultMap delInfo(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) Remindinfo remindinfo, @RequestHeader String token, @RequestHeader String user) {
        if (!JWTUtil.getUsername(token).equals(user)) {
            return resultMap.fail().code(401).message("用户非法请求");
        }
        
        remindMapper.deleteInfo(remindinfo.getId());
        return resultMap.success().code(200).message("成功删除该还款信息");
    }

}

@ApiModel
class Remindinfo {
    @ApiModelProperty(value = "还款id")
    private String id;

    @ApiModelProperty(value = "还款对象")
    @NotEmpty(message="还款对象不能为空")
    @Length(max = 20, message = "还款对象长度限制最多20字符")
    private String payment_target;

    @ApiModelProperty(value = "还款日")
    @NotEmpty(message="还款日不能为空")
    @Length(max = 5, message = "还款日长度限制最多5字符")
    private String payment_date;


    public String getPayment_target() {
        return payment_target;
    }

    public void setPayment_target(String payment_target) {
        this.payment_target = payment_target;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPayment_date() {
        return payment_date;
    }

    public void setPayment_date(String payment_date) {
        this.payment_date = payment_date;
    }
}